package com.zishi.util;


import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author TP
 * @version 1.0
 * @date 2022/5/6/0006 10:42
 * 该类用于将;分隔的内容改为List存储
 * 将List存储的改为;分隔
 */
public class PartitionUtil {

    /**
     * 将;分隔的内容改为List存储
     *
     * @param ids
     * @return
     */
    public static List<Integer> separateCharacters(String ids, Integer intMethod) {
        if (ids == null) {
            return null;
        }

        String[] split = ids.split(ConstUtil.SEPARATOR);
        int[] splitInt = new int[split.length];

        try {
            for (int i = 0; i < split.length; i++) {
//                System.out.println(i+": "+split[i]+"\n\n");
                if (Strings.isEmpty(split[i])) {
                    continue;
                }
                splitInt[i] = Integer.parseInt(split[i]);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return Arrays.stream(splitInt).boxed().collect(Collectors.toList());
    }


    /**
     * 将;分隔的内容改为List存储
     *
     * @param ids
     * @return
     */
    public static List<String> separateCharacters(String ids) {
        if (ids == null) {
            return null;
        }
        String[] split = ids.split(ConstUtil.SEPARATOR);
        ArrayList<String> list = new ArrayList<>();
        for (String s : split) {
            if (Strings.isEmpty(s)) {
                continue;
            }
            list.add(s);
        }
            return list;
    }


        /**
         * 将List存储的改为;分隔
         * @param list
         * @return
         */
        public static String mergeCharacters (List<Integer> list) {

            Iterator<Integer> iterator = list.iterator();
            StringBuilder stringBuilder = new StringBuilder();
            Integer id;
            while (iterator.hasNext()) {
                stringBuilder.append(iterator.next()).append(ConstUtil.SEPARATOR);
            }

            return stringBuilder.toString();
        }
    }
